<template>
  <div class="app-container">
    <!-- 搜索卡片 -->
    <el-card class="search-card" shadow="hover">
      <div slot="header" class="card-header">
        <span class="card-title">
          <i class="el-icon-search"></i>
          搜索条件
        </span>
        <div class="header-actions">
          <el-button type="text" @click="showSearch = !showSearch">
            {{ showSearch ? '收起' : '展开' }}
            <i :class="showSearch ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i>
          </el-button>
          <el-button type="text" @click="resetQuery" icon="el-icon-refresh">重置</el-button>
        </div>
      </div>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="子件名称" prop="componentName">
              <el-input v-model="queryParams.componentName" placeholder="请输入子件名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="子件规格" prop="specification">
              <el-input v-model="queryParams.specification" placeholder="请输入子件规格" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
          </el-col>
          
          <el-col :span="3" style="display: flex; justify-content: flex-end; align-items: center;">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 操作按钮卡片 -->
    <el-card class="action-card" shadow="hover">
      <div class="action-buttons">
        <div class="left-buttons">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['bom:erp_component_bom:add']">新增</el-button>
          <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['bom:erp_component_bom:edit']">修改</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['bom:erp_component_bom:remove']">删除</el-button>
          <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['bom:erp_component_bom:export']">导出</el-button>
        </div>
        <div class="right-buttons">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </div>
      </div>
    </el-card>

    <!-- 数据展示卡片 -->
    <el-card class="data-card" shadow="hover">
      <div slot="header" class="card-header">
        <span class="card-title">
          <i class="el-icon-s-operation"></i>
          子件BOM列表
        </span>
        <span class="card-subtitle">共 {{ total }} 条记录</span>
      </div>
      <el-table v-loading="loading" :data="erp_component_bomList" @selection-change="handleSelectionChange" stripe border class="custom-table">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="子件编码" align="center" prop="componentId" />
        <el-table-column label="子件名称" align="center" prop="componentName" />
        <el-table-column label="子件规格" align="center" prop="specification" />
        <el-table-column label="物料分类" align="center" prop="category" />
        <el-table-column label="材料类型" align="center" prop="materialType" />
        <el-table-column label="表面处理" align="center" prop="surfaceTreatment" />
        <el-table-column label="单位" align="center" prop="unit" />
        <el-table-column label="属性" align="center" prop="attribute" />
        <el-table-column label="基本用量" align="center" prop="baseQuantity" />
        <el-table-column label="子件损耗率" align="center" prop="wastageRate" />
        <el-table-column label="备注" align="center" prop="remarks" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['bom:erp_component_bom:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['bom:erp_component_bom:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <!-- 添加或修改子件BOM对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="子件名称" prop="componentName">
          <el-input v-model="form.componentName" placeholder="请输入子件名称" />
        </el-form-item>
        <el-form-item label="子件规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入子件规格" />
        </el-form-item>
        
        <el-form-item label="物料分类" prop="category">
          <el-input v-model="form.category" placeholder="请输入物料分类" />
        </el-form-item>
        <el-form-item label="表面处理" prop="surfaceTreatment">
          <el-input v-model="form.surfaceTreatment" placeholder="请输入表面处理" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="属性" prop="attribute">
          <el-input v-model="form.attribute" placeholder="请输入属性" />
        </el-form-item>
        <el-form-item label="基本用量" prop="baseQuantity">
          <el-input-number v-model="form.baseQuantity" placeholder="请输入基本用量" :min="0" :precision="2" style="width: 100%" />
        </el-form-item>
        <el-form-item label="子件损耗率" prop="wastageRate">
          <el-input-number v-model="form.wastageRate" placeholder="请输入子件损耗率" :min="0" :max="100" :precision="2" style="width: 100%" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listErp_component_bom, getErp_component_bom, delErp_component_bom, addErp_component_bom, updateErp_component_bom } from "@/api/bom/erp_component_bom"

export default {
  name: "Erp_component_bom",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 子件BOM表格数据
      erp_component_bomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        componentName: null,
        specification: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        componentName: [
          { required: true, message: "子件名称不能为空", trigger: "blur" }
        ],
        specification: [
          { required: true, message: "子件规格不能为空", trigger: "blur" }
        ],
        category: [
          { required: true, message: "物料分类不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        baseQuantity: [
          { required: true, message: "基本用量不能为空", trigger: "blur" },
          { type: "number", message: "基本用量必须为数字", trigger: "blur" }
        ],
        wastageRate: [
          { required: true, message: "子件损耗率不能为空", trigger: "blur" },
          { type: "number", message: "子件损耗率必须为数字", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询子件BOM列表 */
    getList() {
      this.loading = true
      listErp_component_bom(this.queryParams).then(response => {
        this.erp_component_bomList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        componentId: null,
        componentName: null,
        specification: null,
        category: null,
        materialType: null,
        surfaceTreatment: null,
        unit: null,
        attribute: null,
        baseQuantity: null,
        wastageRate: null,
        remarks: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.componentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加子件BOM"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const componentId = row.componentId || this.ids
      getErp_component_bom(componentId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改子件BOM"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.componentId != null) {
            updateErp_component_bom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addErp_component_bom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const componentIds = row.componentId || this.ids
      this.$modal.confirm('是否确认删除子件BOM编号为"' + componentIds + '"的数据项？').then(function() {
        return delErp_component_bom(componentIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bom/erp_component_bom/export', {
        ...this.queryParams
      }, `erp_component_bom_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 84px);
}

.search-card {
  margin-bottom: 20px;
  border-radius: 12px;
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0;
    .card-title {
      font-size: 16px;
      font-weight: 600;
      color: #303133;
      i {
        margin-right: 8px;
        color: #409eff;
      }
    }
    .header-actions {
      display: flex;
      gap: 10px;
    }
  }
}

.action-card {
  margin-bottom: 20px;
  border-radius: 12px;
  .action-buttons {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
    .left-buttons,
    .right-buttons {
      display: flex;
      gap: 10px;
      flex-wrap: wrap;
    }
  }
}

.data-card {
  border-radius: 12px;
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0;
    .card-title {
      font-size: 16px;
      font-weight: 600;
      color: #303133;
      i {
        margin-right: 8px;
        color: #409eff;
      }
    }
    .card-subtitle {
      font-size: 14px;
      color: #909399;
    }
  }
}

.custom-table {
  border-radius: 8px;
  overflow: hidden;
  ::v-deep .el-table__header-wrapper {
    background-color: #fafafa;
  }
  ::v-deep .el-table__row {
    transition: all 0.3s ease;
    cursor: pointer;
    &:hover {
      background-color: #f5f7fa !important;
    }
  }
  ::v-deep .el-table__cell {
    padding: 12px 0;
  }
}

::v-deep .el-card {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border: none;
  &:hover {
    box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.15);
  }
}

::v-deep .el-button {
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
  &.el-button--primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    &:hover {
      background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
      transform: translateY(-2px);
    }
  }
  &.el-button--success {
    background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
    border: none;
    &:hover {
      background: linear-gradient(135deg, #0f8a7d 0%, #2dd66e 100%);
      transform: translateY(-2px);
    }
  }
  &.el-button--danger {
    background: linear-gradient(135deg, #ff416c 0%, #ff4b2b 100%);
    border: none;
    &:hover {
      background: linear-gradient(135deg, #e63a5f 0%, #e64426 100%);
      transform: translateY(-2px);
    }
  }
  &.el-button--warning {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    border: none;
    &:hover {
      background: linear-gradient(135deg, #d884e2 0%, #d44e5f 100%);
      transform: translateY(-2px);
    }
  }
  &.el-button--info {
    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
    border: none;
    &:hover {
      background: linear-gradient(135deg, #45a1f0 0%, #00e5f0 100%);
      transform: translateY(-2px);
    }
  }
}

::v-deep .el-pagination {
  margin-top: 20px;
  text-align: center;
}

::v-deep .el-dialog {
  border-radius: 12px;
  .el-dialog__header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-radius: 12px 12px 0 0;
    .el-dialog__title {
      color: white;
      font-weight: 600;
    }
  }
}

::v-deep .el-form-item__label {
  font-weight: 500;
  color: #606266;
}

::v-deep .el-input__inner {
  border-radius: 8px;
  border: 1px solid #dcdfe6;
  transition: all 0.3s ease;
  &:focus {
    border-color: #409eff;
    box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  }
}

::v-deep .el-select .el-input__inner {
  border-radius: 8px;
}

::v-deep .el-textarea__inner {
  border-radius: 8px;
  border: 1px solid #dcdfe6;
  transition: all 0.3s ease;
  &:focus {
    border-color: #409eff;
    box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  }
}
</style>
